﻿// Gateway.Api 项目的版权、商标、专利和其他相关权利均受相应法律法规的保护。使用本项目应遵守相关法律法规和许可证的要求。
//
// 本项目主要遵循 MIT 许可证和 Apache 许可证（版本 2.0）进行分发和使用。许可证位于源代码树根目录中的 LICENSE-MIT 和 LICENSE-APACHE 文件。
//
// 不得利用本项目从事危害国家安全、扰乱社会秩序、侵犯他人合法权益等法律法规禁止的活动！任何基于本项目二次开发而产生的一切法律纠纷和责任，我们不承担任何责任！

using Aurora.AI.Domain.Domain;
using Aurora.AI.Gateway.Models.UserLimit;
using Aurora.AI.Gateway.Services;
using Gateway.Infrastructure.Provider.Operator;
using Gateway.IQuery;
using Gateway.IQuery.Department;
using Gateway.IQuery.User;
using Gateway.IQuery.User.Request;
using Gateway.IQuery.User.Result;

namespace Gateway.UI.Api.Controllers.Ai.Gateway;
/// <summary>
/// ai gateway userlimit 服务
/// </summary>
[Route("api/ai/gateway/user/limit")]
[ApiController]
public class AiUserLimitController(UserAILimitService service, IOperatorProvider @operator, IUserQuery userQuery, IDepartmentQuery departmentQuery) : BaseController
{
    /// <summary>
    /// 分页列表
    /// </summary>
    /// <param name="request"></param>
    /// <returns></returns>
    [HttpPost("page")]
    public async Task<PageResult<UserLimitPageListResponse>> GetPageAsync(UserPageListRequest request)
    {
        if (!string.IsNullOrWhiteSpace(request.DepartmentId))
        {
            var departmentIds = await departmentQuery.GetDepartmentIdListByParentId(request.DepartmentId);
            if (departmentIds.Count > 0)
                request.DepartmentIds = departmentIds;
        }

        var data = await userQuery.GetPageList(request);
        List<UserLimitPageListResponse> results = new List<UserLimitPageListResponse>();
        if (data.Rows.Count > 0)
        {
            List<string> userIds = data.Rows.Select(x => x.UserId).ToList();
            List<UserAILimit> listByUserIds = await service.GetListByUserIds(userIds);
            foreach (UserDetailResult item in data.Rows)
            {
                UserLimitPageListResponse limit = new UserLimitPageListResponse
                {
                    UserId = item.UserId,
                    Name = item.UserName,
                    ConsumeToken = 0,
                    RequestCount = 0,
                    ResidualCredit = 0,
                    Id = item.UserId,
                    CreatedTime = item.CreatedTime
                };
                UserAILimit firstOrDefault = listByUserIds.FirstOrDefault(x => x.UserId.Equals(item.UserId));
                if (firstOrDefault != null)
                {
                    limit.ConsumeToken = firstOrDefault.ConsumeToken;
                    limit.RequestCount = firstOrDefault.RequestCount;
                    limit.ResidualCredit = firstOrDefault.ResidualCredit / (decimal)500000;
                }
                results.Add(limit);
            }
        }

        return PageResult<UserLimitPageListResponse>.Create(results, data.Total);
    }
    /// <summary>
    /// 创建
    /// </summary>
    /// <param name="request"></param>
    /// <returns></returns>
    [HttpPost("create")]
    public async Task<bool> CreateAsync(UserLimitCreateRequest request)
    {
        request.OperatorId = @operator.GetOperator().OperatorId;
        return await service.CreateAsync(request);
    }
    /// <summary>
    /// 删除
    /// </summary>
    /// <param name="id">主键</param>
    /// <returns></returns>
    [HttpPost("remove/{id}")]
    public async Task<bool> RemoveAsync(string id)
    {
        return await service.RemoveAsync(id);
    }
}
